package com.game.thrones.controller.output;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.game.thrones.constant.Errors;

/**
 * 响应返回数据工具类
 * 
 */
public class ResultResponseBeanUtil {

	public static <T> BaseOutput<T> success(T data) {
		BaseOutput<T> entity = new BaseOutput<T>();
		entity.setData(data);
		entity.setCode(Errors.SUCCESS.code);
		return entity;
	}

	public static BaseOutput<String> success(String msg) {
		BaseOutput<String> entity = new BaseOutput<String>();
		entity.setData(msg);
		entity.setCode(Errors.SUCCESS.code);
		return entity;
	}

	public static BaseOutput<Void> success() {
		BaseOutput<Void> entity = build();
		entity.setCode(Errors.SUCCESS.code);
		return entity;
	}

	/**
	 * 返回分页信息
	 * 
	 * @param list
	 *            数据信息
	 * @param results
	 *            每页条数
	 * @param page页码，第一页为1
	 * @param total总条数
	 * @param totalpage分页数
	 * @return
	 */
	public static <T> PageOutput<T> pageSuccess(List<T> list, Integer size, Integer page, Integer total) {
		PageOutput<T> entity = new PageOutput<T>();
		entity.setData(list);
		entity.setCode(Errors.SUCCESS.code);
		entity.setSize(size);
		entity.setPage(page);
		entity.setTotal(total);
		return entity;
	}

	private static BaseOutput<Void> build() {
		BaseOutput<Void> entity = new BaseOutput<Void>();
		return entity;
	}

	public static String failToString(Errors errors) {
		Map<String, Object> errorMap = new HashMap<String, Object>();
		errorMap.put("code", errors.code);
		errorMap.put("err", errors.label);
		return errorMap.toString();
	}

	public static <T> BaseOutput<T> fail(Errors errors, String message) {
		BaseOutput<T> entity = new BaseOutput<T>();
		entity.setCode(errors.code);
		entity.setErr(message);
		return entity;
	}

	public static <T> BaseOutput<T> fail(String message) {
		BaseOutput<T> entity = new BaseOutput<T>();
		entity.setCode(Errors.SYSTEM_ERROR.code);
		entity.setErr(message);
		return entity;
	}

	public static <T> BaseOutput<T> fail(Errors errors) {
		BaseOutput<T> entity = new BaseOutput<T>();
		if (errors == null) {
			entity.setCode(9999);
			entity.setErr("未知错误");
			return entity;
		}
		entity.setCode(errors.code);
		entity.setErr(errors.label);
		return entity;
	}

	public static <T> BaseOutput<T> fail(Integer httpStatus, Integer code, String message, HttpServletRequest request) {
		BaseOutput<T> entity = new BaseOutput<T>();
		entity.setCode(code);
		entity.setErr(message);
		return entity;
	}
}
